package com.shopping_center.management.common;

import lombok.Data;

/**
 * 封装统一返回结果
 *
 * @author YeFeng
 * @date 2023/2/2
 */

@Data
public class DataWrapper<T> {

    private Boolean success;

    private String message;

    private Integer code;

    private T data;

    /**
     * 错误
     *
     * @param code    代码
     * @param message 消息
     * @return {@link DataWrapper}
     */
    public static <T> DataWrapper<T> errorOf(Integer code, String message) {
        DataWrapper<T> dataWrapper = new DataWrapper<T>();
        dataWrapper.setCode(code);
        dataWrapper.setMessage(message);
        dataWrapper.setSuccess(false);
        return dataWrapper;
    }

    /**
     * 错误
     *
     * @param errorCode 错误代码
     * @return {@link DataWrapper}
     */
    public static<T> DataWrapper<T> errorOf(ResultCode errorCode) {
        return errorOf(errorCode.getCode(), errorCode.getMessage());
    }

    /**
     * 错误数据
     *
     * @param code    代码
     * @param message 消息
     * @param t       t
     * @return {@link DataWrapper}<{@link T}>
     */
    public static<T> DataWrapper<T> errorWithData(Integer code, String message, T t) {
        DataWrapper<T> dataWrapper = new DataWrapper<T>();
        dataWrapper.setCode(code);
        dataWrapper.setSuccess(false);
        dataWrapper.setMessage(message);
        dataWrapper.setData(t);
        return dataWrapper;
    }

    /**
     * 错误数据
     *
     * @param errorCode 错误代码
     * @param t         t
     * @return {@link DataWrapper}<{@link T}>
     */
    public static<T> DataWrapper<T> errorWithData(ResultCode errorCode, T t) {
        return errorWithData(errorCode.getCode(), errorCode.getMessage(), t);
    }


    /**
     * 成功
     *
     * @param code    代码
     * @param message 消息
     * @return {@link DataWrapper}<{@link T}>
     */
    public static <T> DataWrapper<T> successOf(Integer code, String message) {
        DataWrapper<T> dataWrapper = new DataWrapper<T>();
        dataWrapper.setCode(code);
        dataWrapper.setMessage(message);
        dataWrapper.setSuccess(true);
        return dataWrapper;
    }

    /**
     * 成功
     *
     * @param successCode 成功代码
     * @return {@link DataWrapper}<{@link T}>
     */
    public static<T> DataWrapper<T> successOf(ResultCode successCode) {
        return successOf(successCode.getCode(), successCode.getMessage());
    }

    /**
     * 成功数据
     *
     * @param code    代码
     * @param message 消息
     * @param t       t
     * @return {@link DataWrapper}<{@link T}>
     */
    public static<T> DataWrapper<T> successWithData(Integer code, String message, T t) {
        DataWrapper<T> dataWrapper = new DataWrapper<T>();
        dataWrapper.setCode(code);
        dataWrapper.setSuccess(false);
        dataWrapper.setMessage(message);
        dataWrapper.setData(t);
        return dataWrapper;
    }

    /**
     * 成功数据
     *
     * @param t           t
     * @param successCode 成功代码
     * @return {@link DataWrapper}<{@link T}>
     */
    public static<T> DataWrapper<T> successWithData(ResultCode successCode, T t) {
        return successWithData(successCode.getCode(), successCode.getMessage(), t);
    }
}
